//
//  UserDataViewController.m
//  享动
//
//  Created by darker on 16/4/9.
//  Copyright © 2016年 HHUI. All rights reserved.
//

#import "UserDataViewController.h"
#import "UserViewController.h"
#import "UserModel.h"

@interface UserDataViewController ()

@property (weak, nonatomic) IBOutlet UIView *backView;

@property (weak, nonatomic) IBOutlet UIScrollView *backScrollView;

@property (weak, nonatomic) IBOutlet UIImageView *backImageView;

// 用户头像
@property (weak, nonatomic) IBOutlet UIImageView *userHeadImageView;

// 编辑button
@property (weak, nonatomic) IBOutlet UIButton *editButton;

// 用户昵称
@property (weak, nonatomic) IBOutlet UILabel *userName;

@property (nonatomic, strong) NSMutableArray *modelArray;

@property (weak, nonatomic) IBOutlet UILabel *heightLabel;

@property (weak, nonatomic) IBOutlet UILabel *weightLabel;

@property (weak, nonatomic) IBOutlet UILabel *height;

@property (weak, nonatomic) IBOutlet UILabel *weight;


@end

@implementation UserDataViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    //[self request];
    
    _backScrollView.frame = CGRectMake(0, 0, KWIDTH, 250);
    _backScrollView.contentOffset = CGPointMake(0, 0);
    _backScrollView.contentSize = CGSizeMake(KWIDTH * 2, 250);
    _backScrollView.showsHorizontalScrollIndicator = NO;
    _backScrollView.showsVerticalScrollIndicator = NO;
    _backScrollView.pagingEnabled = YES;
    _backScrollView.bounces = NO;
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    _backView.frame = CGRectMake(0, 0, KWIDTH, 250);
    
    _backImageView.frame = CGRectMake(0, 64, KWIDTH * 2, 250);
    _backImageView.image = [UIImage imageNamed:@"view"];
    
    _userHeadImageView.frame = CGRectMake(10, 150, 50, 50);
    _userHeadImageView.layer.cornerRadius = 25;
    _userHeadImageView.image = [UIImage imageWithData:[[NSUserDefaults standardUserDefaults] objectForKey:@"headImage"]];
    _userHeadImageView.clipsToBounds = YES;
    _userHeadImageView.backgroundColor = [UIColor grayColor];
    
    UILabel *lae = [[UILabel alloc] initWithFrame:CGRectMake(KWIDTH + 50, 67 + 30, 50, 20)];
    lae.text = @"11111";
    
    _heightLabel.frame = CGRectMake(KWIDTH + 50, 64 + 50, 100, 30);
    _weightLabel.frame = CGRectMake(KWIDTH +50, 64 + 50 + 40, 100, 30);
    
    _editButton.frame = CGRectMake(KWIDTH - 60, 64 + 50, 50, 20);
    _editButton.titleLabel.textColor = [UIColor blueColor];
    _editButton.layer.cornerRadius = 10;
    _editButton.layer.borderWidth = 0.3;
    [_editButton addTarget:self action:@selector(editButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    _userName.frame = CGRectMake(65, 165, 150, 20);
    _userName.font = [UIFont systemFontOfSize:14];
    _userName.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"userName"];
    _height.frame = CGRectMake(KWIDTH + 50 + 80, 64 + 50, 100, 30);
    _weight.frame = CGRectMake(KWIDTH + 50 + 80, 64 + 50 + 40, 100, 30);
    _height.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"userHeight"];
    _weight.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"userWeight"];
    
    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:self action:@selector(leftButtonAction)];
    self.navigationItem.leftBarButtonItem = leftButton;
    

}

/*
#pragma mark --- 数据请求 ---
- (void) request
{
    
    NSDictionary *parDic = @{@"_ch":@"appStore", @"_lan":@"ch", @"_nw":@"WiFi", @"_product":@"fitmix", @"_sdk":@"iPhone%20OS8.2",@"_v":@"3",@"email":[[NSUserDefaults standardUserDefaults] objectForKey:@"name"], @"password":[[NSUserDefaults standardUserDefaults] objectForKey:@"password"], @"uid":@"0"};
    
    [RequestManager requestWithUrl:@"http://appt.igeekery.com/user/login.json" requestType:RequestTypePost parDic:parDic finish:^(NSData *data)
     {
         NSDictionary *dataDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
         
         NSDictionary *userDic = [dataDic objectForKey:@"user"];
         
         UserModel *model = [[UserModel alloc] init];
         
         _modelArray = [NSMutableArray array];
         
         [model setValuesForKeysWithDictionary:userDic];
         
         [_modelArray addObject:model];
         
         dispatch_async(dispatch_get_main_queue(), ^{
             _userName.frame = CGRectMake(65, 165, 150, 20);
             _userName.font = [UIFont systemFontOfSize:14];
             _userName.text = model.name;
             _height.frame = CGRectMake(KWIDTH + 50 + 80, 64 + 50, 100, 30);
             _weight.frame = CGRectMake(KWIDTH + 50 + 80, 64 + 50 + 40, 100, 30);
             _height.text = [NSString stringWithFormat:@"%ldcm", model.height];
             _weight.text = [NSString stringWithFormat:@"%ldkg", model.weight];
         });
     } error:^(NSError *error) {
         NSLog(@"数据请求错误");
     }];
}
 */

- (void)leftButtonAction
{
    if (_myUserName != nil)
    {
        _userHeadImage(_userHeadImageView.image);
    }
    if (_myUserName != nil)
    {
        _myUserName(_userName.text);
    }
    
    // 创建CATransition对象
    CATransition *animation = [CATransition animation];
    
    //设置运动时间
    animation.duration = 1;
    
    //设置运动type
    animation.type = kCATransitionFromLeft;
    
    //设置运动速度
    animation.timingFunction = UIViewAnimationOptionCurveEaseInOut;
    
    [self.navigationController.view.layer addAnimation:animation forKey:nil];
    
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark --- 点击编辑button跳转 --- 
- (void)editButtonAction:(UIButton *)sender
{
    // 创建CATransition对象
    CATransition *animation = [CATransition animation];
    
    //设置运动时间
    animation.duration = 1;
    
    //设置运动type
    animation.type = kCATransitionFromRight;
    
    //设置运动速度
    animation.timingFunction = UIViewAnimationOptionCurveEaseInOut;
    
    [self.navigationController.view.layer addAnimation:animation forKey:nil];
    
    UserViewController *userTableVc = [[UserViewController alloc] initWithNibName:@"User" bundle:[NSBundle mainBundle]];
    
    [self.navigationController pushViewController:userTableVc animated:YES];
    __weak UserDataViewController *blockSelf = self;
    
    userTableVc.userHeadImage = ^(UIImage *image){
        // 只有调用block的时候才运行这行代码
        blockSelf.userHeadImageView.image = image;
    };
    userTableVc.userName = ^(NSString *userName){
        // 只有调用block的时候才运行这行代码
        blockSelf.userName.text = userName;
    };
    userTableVc.userWeight = ^(NSInteger weight){
        // 只有调用block的时候才运行这行代码
        blockSelf.weight.text = [NSString stringWithFormat:@"%ldkg", weight];
    };
    userTableVc.userHeight = ^(NSInteger height){
        // 只有调用block的时候才运行这行代码
        blockSelf.height.text = [NSString stringWithFormat:@"%ldcm", height];
    };
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
